package com.sunstar.club.subject.domain.handler.subject.impl;

import com.sunstar.club.subject.common.enums.IsDeletedFlagEnum;
import com.sunstar.club.subject.common.enums.SubjectInfoTypeEnum;
import com.sunstar.club.subject.domain.convert.JudgeConverter;
import com.sunstar.club.subject.domain.entity.SubjectAnswerBo;
import com.sunstar.club.subject.domain.entity.SubjectInfoBo;
import com.sunstar.club.subject.domain.entity.SubjectOptionBo;
import com.sunstar.club.subject.domain.handler.subject.SubjectTypeHandler;
import com.sunstar.club.subject.infra.basic.entity.SubjectJudge;
import com.sunstar.club.subject.infra.basic.service.SubjectJudgeService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 判断题目的策略类
 *
 * @Author: Shylo Sun
 * @Project: track-club
 * @Date: 2024/2/24 11:14
 */
@Component
public class JudgeTypeHandler implements SubjectTypeHandler {

    @Resource
    private SubjectJudgeService subjectJudgeService;

    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.JUDGE;
    }

    @Override
    public void add(SubjectInfoBo subjectInfoBo) {
        // 判断题目的插入
        SubjectJudge subjectJudge = new SubjectJudge();
        SubjectAnswerBo subjectAnswerBo = subjectInfoBo.getOptionList().get(0);
        subjectJudge.setSubjectId(subjectInfoBo.getId());
        subjectJudge.setIsCorrect(subjectAnswerBo.getIsCorrect());
        subjectJudge.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        subjectJudgeService.insert(subjectJudge);
    }

    @Override
    public SubjectOptionBo query(int subjectId) {
        SubjectJudge subjectJudge = new SubjectJudge();
        subjectJudge.setSubjectId(Long.valueOf(subjectId));
        List<SubjectJudge> result = subjectJudgeService.queryByCondition(subjectJudge);
        List<SubjectAnswerBo> subjectAnswerBoList = JudgeConverter.INSTANCE.convertJudgeListToBo(result);
        SubjectOptionBo subjectOptionBo = new SubjectOptionBo();
        subjectOptionBo.setOptionList(subjectAnswerBoList);
        return subjectOptionBo;
    }
}
